﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using InTheHand.Bluetooth;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfBluetooth.ViewModel
{
    public class BluetoothServer : ObservableObject
    {
        /// <summary>
        /// 蓝牙服务
        /// </summary>
        private Guid _sever;
        /// <summary>
        /// 蓝牙服务
        /// </summary>
        public Guid sever { get => _sever; set => SetProperty(ref _sever, value); }

        /// <summary>
        /// 蓝牙
        /// </summary>
        private BluetoothClient _client;
        /// <summary>
        /// 蓝牙
        /// </summary>
        public BluetoothClient client { get => _client; set => SetProperty(ref _client, value); }

        /// <summary>
        /// 蓝牙
        /// </summary>
        private BluetoothDeviceInfo _bluetoothDeviceInfo;
        /// <summary>
        /// 蓝牙
        /// </summary>
        public BluetoothDeviceInfo bluetoothDeviceInfo { get => _bluetoothDeviceInfo; set => SetProperty(ref _bluetoothDeviceInfo, value); }
        /// <summary>
        /// 写入信息
        /// </summary>
        private string _writeInfo;
        /// <summary>
        /// 写入信息
        /// </summary>
        public string writeInfo { get => _writeInfo; set => SetProperty(ref _writeInfo, value); }

        /// <summary>
        /// 读取信息
        /// </summary>
        private string _readInfo;
        /// <summary>
        /// 读取信息
        /// </summary>
        public string readInfo { get => _readInfo; set => SetProperty(ref _readInfo, value); }

        /// <summary>
        /// 链接
        /// </summary>
        private bool _Connected;
        /// <summary>
        /// 链接
        /// </summary>
        public bool Connected { get => _Connected; set => SetProperty(ref _Connected, value); }

        /// <summary>
        /// 蓝牙名称
        /// </summary>
        private string _blueName;
        /// <summary>
        /// 蓝牙名称
        /// </summary>
        public string blueName { get => _blueName; set => SetProperty(ref _blueName, value); }


        /// <summary>
        /// 链接
        /// </summary>
        public ICommand Connect { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public ICommand UnConnect { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public ICommand Read { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public ICommand Write { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public BluetoothServer()
        {
            
            client = new BluetoothClient();
            
            Connect = new RelayCommand(ConnectMethod);
            UnConnect = new RelayCommand(UnConnectMethod);
            Read = new RelayCommand(ReadMethod);
            Write = new RelayCommand(WriteMethod);

           
        }



        private void UnConnectMethod()
        {
            if (client.Connected)
            {
                client.Close();
            }
            Connected = client.Connected;
        }

        private void WriteMethod()
        {
            if (client.Connected == false)
            {
                return;
            }
            if (string.IsNullOrEmpty(writeInfo))
            {
                return;
            }
            try
            {
                Task.Factory.StartNew(() =>
                {

                    var steam = client.GetStream();
                    if (steam.CanWrite)
                    {
                        var bytedata = UTF8Encoding.UTF8.GetBytes(writeInfo);
                        steam.Write(bytedata);
                    }
                    steam.Close();
                });

            }
            catch (Exception ex)
            {
                readInfo = ex.Message;
            }
        }

        private void ReadMethod()
        {
            if (client.Connected == false)
            {
                return;
            }
            try
            {
                Task.Factory.StartNew(() =>
                {
                    var steam = client.GetStream();
                    if (steam.CanRead)
                    {
                        var lenght = steam.ReadByte();
                        if (lenght > 0)
                        {
                            byte[] bytevalue = new byte[steam.ReadByte()];
                            steam.Read(bytevalue);
                            readInfo = Encoding.UTF8.GetString(bytevalue);
                        }
                        else
                        {
                            readInfo = $"{sever}-无数据";
                        }
                    }
                    else
                    {
                        readInfo = $"不能读取{sever}";
                    }
                    steam.Close();
                });

            }
            catch (Exception ex)
            {
                readInfo = ex.Message;
            }
        }

        private void ConnectMethod()
        {
            
            try
            {
                if (client.Connected == false)
                {
                    var endpoint = new BluetoothEndPoint(bluetoothDeviceInfo.DeviceAddress, sever);
                    client.Connect(endpoint);
                }
            }
            catch (Exception ex)
            {
                readInfo = ex.Message;
            }
            Connected = client.Connected;
        }
    }
}
